package cqut.wys.web;


import cqut.wys.websocket.WebSocketServerExt;
import org.java_websocket.WebSocketImpl;

import javax.servlet.*;
import java.io.IOException;
import java.net.UnknownHostException;

/**
 * 服务开启连接器拦截器
 * @author wuyoushan@zbj.com
 * @date 2017/3/16.
 */
public class StartFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.starWebSocketOnline();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }

    /**
     * 启动socket服务
     */
    public void starWebSocketOnline() {
        System.out.println("开始启动webSocket");
        WebSocketImpl.DEBUG = false;
        int port = 8888;
        WebSocketServerExt s = null;
        try {
            s = new WebSocketServerExt(port);
            s.start();
        } catch (UnknownHostException e) {
            System.out.println("启动webSocket失败！");
            e.printStackTrace();
        }
        System.out.println("启动webSocket成功！");
    }
}
